package instruments;

import data.ConnectionPoint;
import data.NavmeshRegion;
import data.FRectangle;
import data.Params;
import data.RegionConnection;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sad
 */
public class Recalculator {
    private List<NavmeshRegion> polygons;

    public Recalculator(List<NavmeshRegion> polygons) {
        this.polygons = polygons;
    }

    private void clear() {
        for (NavmeshRegion polygon : polygons) {
            polygon.setConnections(null);
        }
    }

    public void recalculate() {
        clear();
        for (int j = 0; j < polygons.size(); j++) {
            NavmeshRegion p1 = polygons.get(j);
            FRectangle rect = new FRectangle(p1.getX(), p1.getY(), p1.getWidth(), p1.getHeight());
            for (int i = 0; i < polygons.size(); i++) {
                if (i == j) {
                    continue;
                }

                NavmeshRegion p2 = polygons.get(i);
                if ((p2.getConnection() != null && p1.getConnection() != null) && (p2.getConnection().get(p1) != null || p1.getConnection().get(p2) != null)) {
                    continue;
                }

                Rectangle rect1 = new Rectangle((int) p2.getX(), (int) p2.getY(), (int) p2.getWidth(), (int) p2.getHeight());
                if (rect.intersects(rect1)) {
                    findCollissionPoints(p1, p2);
                }
            }
        }
    }

    protected void findCollissionPoints(NavmeshRegion p1, NavmeshRegion p2) {
        double x = p1.getX() + Params.CELL_SIZE / 2;
        double y = p1.getY() + Params.CELL_SIZE / 2;
        int hcount = p1.getHorizontalCellsCount();
        int vcount = p1.getVerticalCellsCount();
        RegionConnection connection = new RegionConnection();
        List<ConnectionPoint> connectionPoints = new ArrayList<>();
        connection.setConnectionPoints(connectionPoints);
        RegionConnection connectionInAnotherPolygon = new RegionConnection();
        List<ConnectionPoint> connectionPointsInAnotherPolygon = new ArrayList<>();
        connectionInAnotherPolygon.setConnectionPoints(connectionPointsInAnotherPolygon);
        double tx;
        double ty = y;
        for (int j = 0; j < vcount; j++) {
            tx = x;
            for (int i = 0; i < hcount; i++, tx += Params.CELL_SIZE) {
                if (p1.isActiveCell(i, j) && p2.isActiveCell(tx, ty)) {
                    Point currentConnectedPoint = new Point(i, j);
                    Point connectedPointInAnotherPolygon = new Point();
                    p2.getIndexOfCell(tx, ty, connectedPointInAnotherPolygon);
                    ConnectionPoint cp = new ConnectionPoint(currentConnectedPoint, connectedPointInAnotherPolygon);
                    connectionPoints.add(cp);
                    ConnectionPoint cp2 = new ConnectionPoint(connectedPointInAnotherPolygon, currentConnectedPoint);
                    connectionPointsInAnotherPolygon.add(cp2);
                }
            }

            ty += Params.CELL_SIZE;
        }

        if (!connectionPoints.isEmpty()) {
            p1.addConnectedPolygon(p2, connection);
            p2.addConnectedPolygon(p1, connectionInAnotherPolygon);
        }
    }
}
